package com.kfm.company.provider;

import com.kfm.company.model.Goods;
import org.apache.ibatis.jdbc.SQL;

public class GoodsProvider {

    public String selectAll() {
        return "select * from goods";
    }


    public String selectByGoods(Goods goods) {
        SQL sql = new SQL();
        // select id, goods_name, goods_price, category_id
        // sql.SELECT("id", "goods_name", "goods_price", "category_id");
        // select *
        sql.SELECT("*");
        sql.FROM("goods");
        if (goods!=null) {
            if (goods.getId() != null) {
                sql.WHERE("id="+ goods.getId());
            }
            if (goods.getPrice() != null) {
                sql.WHERE("price="+ goods.getPrice());
            }
            if (goods.getName() != null) {
                sql.WHERE("name="+ goods.getName());
            }
            if (goods.getCategoryId() != null) {
                sql.WHERE("category_id="+ goods.getCategoryId());
            }
        }
        return sql.toString();
    }

    public String insert2(Goods goods) {
        SQL sql = new SQL();
        sql.INSERT_INTO("goods");
        if (goods != null) {
            if (goods.getId() != null) {
                sql.INTO_COLUMNS("id");
                sql.INTO_VALUES("#{id}");
            }
            if (goods.getName() != null) {
                sql.INTO_COLUMNS("goods_name");
                sql.INTO_VALUES("#{name}");
            }
            if (goods.getPrice() != null) {
                sql.INTO_COLUMNS("goods_price");
                sql.INTO_VALUES("#{price}");
            }
            if (goods.getCategoryId() != null) {
                sql.INTO_COLUMNS("category_id");
                sql.INTO_VALUES("#{categoryId}");
            }
        }

        return sql.toString();
    }

    public String updateByGoods(Goods goods) {
        return new SQL() {
            // 实例代码块
            {
                // update goods
                UPDATE("goods");
                if (goods != null) {
                    if (goods.getName() != null) {
                        // set goods_name = #{name},
                        SET("goods_name = #{name}");
                    }
                    if (goods.getPrice() != null) {
                        SET("goods_price = #{price}");
                    }
                    if (goods.getCategoryId() != null) {
                        SET("category_id = #{categoryId}");
                    }
                    if (goods.getId() != null) {
                        // where id = #{id}
                        WHERE("id = #{id}");
                    }
                }
            }
        }.toString();
    }

}
